//
//  FileTableViewController.m
//  OfficeEtc
//
//  Created by  on 12-7-4.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "FileTableViewController.h"
#import "FileItemOperation.h"
#import "AppDelegate.h"
#import "RootViewController.h"
#import "AudioMainViewController.h"
#import "ZipArchive.h"
#import "NewNameOrCreaterFloderViewController.h"

#define SearchBarShowDis  -20.f
typedef enum {
    CellContent_Name =0x100,
    CellContent_Time,
    CellContent_SizeOrCount,
    CellContent_Img
}CellContentTag;

@interface FileTableViewController ()

@end

@implementation FileTableViewController
@synthesize m_ListTableView;
@synthesize m_CurrentPath;
@synthesize m_CurrentFilesArray;
@synthesize m_FileImgDic;
@synthesize m_SuperEditItems;
@synthesize m_SuperNoEditItems;
@synthesize m_OprEditMode;
@synthesize m_EditItems;
-(void) dealloc{
    [p_BackUpFilesArray release];
    [p_UnZipPath release];
    [m_EditItems release];
    [p_ZipArchive release];
    [p_FItemOper release];
    [m_CurrentPath release];
    [m_CurrentFilesArray release];
    [p_DocSearchBar release];
    [m_SuperEditItems release];
    [m_SuperNoEditItems release];
    [super dealloc];
}

-(void) viewWillAppear:(BOOL)animated
{
    if (self.navigationItem.rightBarButtonItem == nil) {
        UIBarButtonItem* tEditBarBt = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStyleDone target:self action:@selector(docEditMode:)];
        self.navigationItem.rightBarButtonItem= tEditBarBt;
        [tEditBarBt release];
    }
    if (!m_ListTableView.editing && [m_EditItems count] == 0 ) {
        
        [self flashItemsToShow];
    }
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	
    m_ListTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, self.view.bounds.size.height-84) style:UITableViewStylePlain];
    m_ListTableView.delegate = self;
    m_ListTableView.dataSource = self;
    [self.view addSubview:m_ListTableView];
    
    
    p_DocSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, -40, 320, 40)];
    p_DocSearchBar.delegate = self;
    [m_ListTableView addSubview:p_DocSearchBar];
    
    p_RegistBtt = [UIButton buttonWithType:UIButtonTypeCustom];
    [p_RegistBtt setBackgroundColor:[UIColor blackColor]];
    p_RegistBtt.frame =CGRectMake(0, 39, 320, self.view.bounds.size.height-84);
    p_RegistBtt.alpha =0.4f;
    [p_RegistBtt addTarget:self action:@selector(registSearchInput) forControlEvents:UIControlEventTouchUpInside];
    p_RegistBtt.hidden =YES;
    [self.view addSubview:p_RegistBtt];
    
    UIToolbar* tBottomBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 480-20-40-44, 320, 40)];
    tBottomBar.tag = 30;
    tBottomBar.tintColor = [UIColor lightGrayColor];
    [self.view addSubview:tBottomBar];
    [tBottomBar release];

    UIBarButtonItem* tBarBtItem0 = [[UIBarButtonItem alloc] initWithTitle:@"移动" style:UIBarButtonItemStyleBordered target:self action:@selector(toolButtonPressed:)];
    tBarBtItem0.width =46;
    tBarBtItem0.tag = 20;
    UIBarButtonItem* tBarBtItem1 = [[UIBarButtonItem alloc] initWithTitle:@"粘贴" style:UIBarButtonItemStyleBordered target:self action:@selector(toolButtonPressed:)];
    tBarBtItem1.width =46;
    tBarBtItem1.tag = 21;
    UIBarButtonItem* tBarBtItem2 = [[UIBarButtonItem alloc] initWithTitle:@"复制" style:UIBarButtonItemStyleBordered target:self action:@selector(toolButtonPressed:)];
    tBarBtItem2.width =46;
    tBarBtItem2.tag = 22;
    UIBarButtonItem* tBarBtItem3 = [[UIBarButtonItem alloc] initWithTitle:@"删除" style:UIBarButtonItemStyleBordered target:self action:@selector(toolButtonPressed:)];
    tBarBtItem3.width =46;
    tBarBtItem3.tag = 23;
    UIBarButtonItem* tBarBtItem4 = [[UIBarButtonItem alloc] initWithTitle:@"压缩" style:UIBarButtonItemStyleBordered target:self action:@selector(toolButtonPressed:)];
    tBarBtItem4.width =46;
    tBarBtItem4.tag = 24;
    UIBarButtonItem* tBarBtItem5 = [[UIBarButtonItem alloc] initWithTitle:@"重命名" style:UIBarButtonItemStyleBordered target:self action:@selector(toolButtonPressed:)];
    tBarBtItem5.width =56;
    tBarBtItem5.tag = 25;
    m_SuperNoEditItems =[[NSMutableArray alloc] initWithObjects:tBarBtItem0,tBarBtItem1,tBarBtItem2,tBarBtItem3,tBarBtItem4,tBarBtItem5,nil];
    
    [tBarBtItem0 release];
    [tBarBtItem1 release];
    [tBarBtItem2 release];
    [tBarBtItem3 release];
    [tBarBtItem4 release];
    [tBarBtItem5 release];

    UIBarButtonItem* tBarBtItem10 = [[UIBarButtonItem alloc] initWithTitle:@"wifi" style:UIBarButtonItemStyleBordered target:self action:@selector(toolButtonPressed:)];
    [tBarBtItem10 setTintColor:[UIColor blackColor]];
    tBarBtItem10.width =98;
    tBarBtItem10.tag = 30;
    UIBarButtonItem* tBarBtItem11 = [[UIBarButtonItem alloc] initWithTitle:@"ftp" style:UIBarButtonItemStyleBordered target:self action:@selector(toolButtonPressed:)];
    [tBarBtItem11 setTintColor:[UIColor blackColor]];
    tBarBtItem11.width =98;
    tBarBtItem11.tag = 31;
    UIBarButtonItem* tBarBtItem12 = [[UIBarButtonItem alloc] initWithTitle:@"新建文件夹" style:UIBarButtonItemStyleBordered target:self action:@selector(toolButtonPressed:)];
    [tBarBtItem12 setTintColor:[UIColor blackColor]];
    tBarBtItem12.width =108;
    tBarBtItem12.tag = 32;
    m_SuperEditItems =[[NSMutableArray alloc] initWithObjects:tBarBtItem10,tBarBtItem11,tBarBtItem12,nil];
    [tBarBtItem10 release];
    [tBarBtItem11 release];
    [tBarBtItem12 release];
    
    m_CurrentFilesArray = [[NSMutableArray alloc] init];
    
    p_FItemOper = [[FileItemOperation alloc] init];
    
    p_ZipArchive = [[ZipArchive alloc] init];
    
    if (m_EditItems == nil) {
        m_EditItems = [[NSMutableArray alloc] init];
    }
    
    if (m_OprEditMode != ItemOperNoneMode) {
        [tBottomBar setItems:m_SuperNoEditItems];
        UIToolbar* tBottomBar = (UIToolbar*)[self.view viewWithTag:30];
        NSMutableArray* tMItems = [NSMutableArray arrayWithArray:tBottomBar.items];
        for (UIBarButtonItem* tGrayBar in tMItems) {
            [tGrayBar setTintColor:[UIColor lightGrayColor]];
        }
        UIBarButtonItem* tItem0 =[tMItems objectAtIndex:1];
        [tItem0 setTintColor:[UIColor blackColor]];
        [tBottomBar setItems:tMItems animated:YES];
        self.m_CurrentFilesArray= [NSMutableArray arrayWithArray:[p_FItemOper getCurrentFloderList:m_CurrentPath]];
        [m_ListTableView reloadData];
    }else {
        [tBottomBar setItems:m_SuperEditItems];
    }
    
    

}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.

}
#pragma mark-
#pragma mark 类私函数

-(void) setM_CurrentFilesArray:(NSMutableArray *)f_NewCurr
{
    [m_CurrentFilesArray release];
    m_CurrentFilesArray = nil;
    m_CurrentFilesArray = f_NewCurr;
    [m_CurrentFilesArray retain];
    
    [p_BackUpFilesArray release];
    p_BackUpFilesArray = nil;
    p_BackUpFilesArray = [NSMutableArray arrayWithArray:f_NewCurr];
    [p_BackUpFilesArray retain];
}

-(void) unArchiveZipFile:(NSString*) f_ZipPath
{
    NSString* tNames = [f_ZipPath lastPathComponent];
    NSString* tFrontPath = [f_ZipPath stringByDeletingLastPathComponent];
    NSString* tizipto = [tNames substringWithRange:NSMakeRange(0, [tNames length]-4)];
    NSString* tTotallPath = [tFrontPath stringByAppendingPathComponent:tizipto];
    
    UIAlertView* tAlertView = [[UIAlertView alloc] initWithTitle:@"错误提示" message:@"解压缩文件失败！" delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles: nil];
    if( [p_ZipArchive UnzipOpenFile:f_ZipPath] )
    {
        BOOL ret = [p_ZipArchive UnzipFileTo:tTotallPath overWrite:YES];
        if( NO==ret )
        {
            NSLog(@"unarchive file error =%@",f_ZipPath);
            [tAlertView show];
        }else {
            [self flashItemsToShow];
        }
        [p_ZipArchive UnzipCloseFile];
    }else {
        [tAlertView show];
    }
    [tAlertView release];
}

-(void) archiveItems:(NSArray*)f_ArrDic zipName:(NSString*)f_ZipName
{    
    BOOL ret = [p_ZipArchive CreateZipFile2:f_ZipName];
    UIAlertView* tAlertView = [[UIAlertView alloc] initWithTitle:@"错误提示" message:@"压缩文件失败！" delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles: nil];
    
    NSFileManager* tFileManager = [NSFileManager defaultManager];
    for (NSDictionary* tAddItem in f_ArrDic) {
        NSString* tPath = [tAddItem objectForKey:ItemDic_Path];
        NSString* tName = [tPath lastPathComponent];
        BOOL tFloder = NO;
        if ([tFileManager fileExistsAtPath:tPath isDirectory:&tFloder] && !tFloder) {
            ret = [p_ZipArchive addFileToZip:tPath newname:tName];
        }else {
            [p_ZipArchive addFolderToZip:tPath pathPrefix:nil];
        }
        if (!ret) {
            NSLog(@"add file to zip error =%@",tPath);
            [tAlertView show];
            break;
        }
    }
    if( ![p_ZipArchive CloseZipFile2] )
    {
        
    }
    [self flashItemsToShow];
    [tAlertView release];
}

-(void) registSearchInput
{
    p_RegistBtt.hidden =YES;
    [p_DocSearchBar setShowsCancelButton:NO animated:YES];
    [p_DocSearchBar resignFirstResponder];
    
}

-(void) toolButtonPressed:(UIBarButtonItem*)f_sender
{
    
    switch (f_sender.tag) {
        case 30://导入wifi
        {
            m_OprEditMode = ItemOperNoneMode;
            WifiViewController* tWifiVC = [[WifiViewController alloc] init];
            if ([tWifiVC networkAvailable]) {
                tWifiVC.m_OperationPath =m_CurrentPath;
                tWifiVC.m_Delegate = self;
                UINavigationController* tRootNav = [[UINavigationController alloc] initWithRootViewController:tWifiVC];
                [tWifiVC release];
                [self presentModalViewController:tRootNav animated:YES];
                [tRootNav release];
            }else {
                [tWifiVC release];
                UIAlertView* tAlertWifi = [[UIAlertView alloc] initWithTitle:@"友情提示" message:@"没有检测到wifi信号请稍后再试！" delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles: nil];
                [tAlertWifi show];
                [tAlertWifi release];
            }
        }
            break;
        case 31://ftp
        {
            m_OprEditMode = ItemOperNoneMode;
            AppDelegate* tAppDel = (AppDelegate*)[UIApplication sharedApplication].delegate;
            UINavigationController* tRootNav = (UINavigationController*)tAppDel.window.rootViewController;
            RootViewController* tRootVC = (RootViewController*)[tRootNav.viewControllers objectAtIndex:0];
            [tRootVC FTPServerShow:m_CurrentPath];
        }
            break;
        case 32://新建文件夹
        {
            m_OprEditMode = ItemOperNoneMode;
            NewNameOrCreaterFloderViewController* tNFLViewController = [[NewNameOrCreaterFloderViewController alloc] init];
            tNFLViewController.m_OperMode = OperMode_CreateFloder;
            tNFLViewController.m_OperPath = m_CurrentPath;
            [self.navigationController pushViewController:tNFLViewController animated:YES];
            [tNFLViewController release];
            
        }
            break;
        case 20://移动
        {
            if ([m_EditItems count] > 0) {
                UIBarButtonItem* tEditBar =  self.navigationItem.rightBarButtonItem;
                [tEditBar setTitle:@"取消"];
                for (id tFuckCC in self.navigationController.viewControllers) {
                    if ([tFuckCC isKindOfClass:[FileTableViewController class]]) {
                        FileTableViewController* tViewCC = (FileTableViewController*)tFuckCC;
                        tViewCC.m_OprEditMode =ItemOperMoveMode;
                        [tViewCC checkBottomButton];
                        [tViewCC.m_ListTableView setEditing:NO animated:YES];
                        UIToolbar* tBottomBar = (UIToolbar*)[tViewCC.view viewWithTag:30];
                        NSMutableArray* tMItems = [NSMutableArray arrayWithArray:tBottomBar.items];
                        for (UIBarButtonItem* tGrayBar in tMItems) {
                            [tGrayBar setTintColor:[UIColor lightGrayColor]];
                        }
                        UIBarButtonItem* tItem0 =[tMItems objectAtIndex:1];
                        [tItem0 setTintColor:[UIColor blackColor]];
                        [tBottomBar setItems:tMItems animated:YES];
                    }
                }
            }
        }
            break;
        case 21://粘贴
        {
            if ([m_EditItems count] >= 1) {
                for (NSDictionary* tItemDic in m_EditItems) {
                    NSString* tSrcPath = [tItemDic objectForKey:ItemDic_Path];
                    if ([m_CurrentPath rangeOfString:tSrcPath].location != NSNotFound) {
                        UIAlertView* tAlerView = [[UIAlertView alloc] initWithTitle:@"错误路径" message:@"当前路径无法进行操作" delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles: nil];
                        [tAlerView show];
                        [tAlerView release];
                        return;
                    }
                    NSFileManager* tFManager = [NSFileManager defaultManager];
                    NSError* tError = nil;
                    NSArray* tContents = [tFManager contentsOfDirectoryAtPath:m_CurrentPath error:&tError];
                    if (tError != nil) {
                        NSLog(@"error =%@",[tError description]);
                        return;
                    }
                    BOOL tIsSameFile = NO;
                    for (NSString* tSubPath in tContents) {
                        NSString* tSubName = [tSubPath lastPathComponent];
                        if ([tSubName isEqualToString:[tSrcPath lastPathComponent]]) {
                            tIsSameFile =YES;
                            break;
                        }
                    }
                    if (tIsSameFile) {
                        UIAlertView* tAlerView = [[UIAlertView alloc] initWithTitle:@"错误路径" message:@"当前路径下有同名文件" delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles: nil];
                        [tAlerView show];
                        [tAlerView release];
                        return;
                    }
                    if (m_OprEditMode == ItemOperMoveMode) {
                        AppDelegate* tAppDel = (AppDelegate*)[UIApplication sharedApplication].delegate;
                        UINavigationController* tRootNav = (UINavigationController*)tAppDel.window.rootViewController;
                        RootViewController* tRootVC = (RootViewController*)[tRootNav.viewControllers objectAtIndex:0];
                        if ([tSrcPath isEqualToString:tRootVC.m_AudioVC.m_MusicPath]) {
                            UIAlertView* tAlerV = [[UIAlertView alloc] initWithTitle:@"警告" message:@"音乐根目录不能移动！" delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles:nil];
                            [tAlerV show];
                            [tAlerV release];
                            return;
                        }
                        if ([tSrcPath rangeOfString:@"OfficeEtc.sqlite"].location !=NSNotFound) {
                            UIAlertView* tAlerV = [[UIAlertView alloc] initWithTitle:@"警告" message:@"软件重要数据文件不能移动！" delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles:nil];
                            [tAlerV show];
                            [tAlerV release];
                            return;
                        }
                        [p_FItemOper moveItmeAtPath:tSrcPath toDestinationItemAtPath:m_CurrentPath];
                    }else if(m_OprEditMode == ItemOperCopyMode){
                        [p_FItemOper copyItmeAtPath:tSrcPath toDestinationItemAtPath:m_CurrentPath];
                    }
                }
                [m_EditItems removeAllObjects];
                [self.navigationItem.rightBarButtonItem setTitle:@"完成"];
                
                
                for (id tFuckCC in self.navigationController.viewControllers) {
                    if ([tFuckCC isKindOfClass:[FileTableViewController class]]) {
                        FileTableViewController* tViewCC = (FileTableViewController*)tFuckCC;
                        tViewCC.m_OprEditMode =ItemOperNoneMode;
                        [tViewCC flashItemsToShow];
                        [tViewCC.m_ListTableView setEditing:YES animated:YES];
                    }
                }
            }else {
                NSLog(@"move or copy file src error =%@",[m_EditItems description]);
            }
        }
            break;
        case 22://复制
        {
            if ([m_EditItems count] > 0) {
                UIBarButtonItem* tEditBar =  self.navigationItem.rightBarButtonItem;
                [tEditBar setTitle:@"取消"];
                for (id tFuckCC in self.navigationController.viewControllers) {
                    if ([tFuckCC isKindOfClass:[FileTableViewController class]]) {
                        FileTableViewController* tViewCC = (FileTableViewController*)tFuckCC;
                        tViewCC.m_OprEditMode =ItemOperCopyMode;
                        [tViewCC checkBottomButton];
                        [tViewCC.m_ListTableView setEditing:NO animated:YES];
                        UIToolbar* tBottomBar = (UIToolbar*)[tViewCC.view viewWithTag:30];
                        NSMutableArray* tMItems = [NSMutableArray arrayWithArray:tBottomBar.items];
                        for (UIBarButtonItem* tGrayBar in tMItems) {
                            [tGrayBar setTintColor:[UIColor lightGrayColor]];
                        }
                        UIBarButtonItem* tItem0 =[tMItems objectAtIndex:1];
                        [tItem0 setTintColor:[UIColor blackColor]];
                        [tBottomBar setItems:tMItems animated:YES];
                    }
                }
            }
        }
            break;
        case 23://删除
        {
            if ([m_EditItems count] > 0) {
                NSFileManager* tFManager = [NSFileManager defaultManager];
                for (NSDictionary* tDelItem in m_EditItems) {
                    NSString* tDelPath = [tDelItem objectForKey:ItemDic_Path];
                    
                    AppDelegate* tAppDel = (AppDelegate*)[UIApplication sharedApplication].delegate;
                    UINavigationController* tRootNav = (UINavigationController*)tAppDel.window.rootViewController;
                    RootViewController* tRootVC = (RootViewController*)[tRootNav.viewControllers objectAtIndex:0];
                    if ([tDelPath isEqualToString:tRootVC.m_AudioVC.m_MusicPath]) {
                        UIAlertView* tAlerV = [[UIAlertView alloc] initWithTitle:@"警告" message:@"音乐根目录不能删除！" delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles:nil];
                        [tAlerV show];
                        [tAlerV release];
                        return;
                    }
                    if ([tDelPath rangeOfString:@"OfficeEtc.sqlite"].location !=NSNotFound) {
                        UIAlertView* tAlerV = [[UIAlertView alloc] initWithTitle:@"警告" message:@"软件重要数据文件不能删除！" delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles:nil];
                        [tAlerV show];
                        [tAlerV release];
                        return;
                    }
                    NSError* tError = nil;
                    BOOL tDelOk = [tFManager removeItemAtPath:tDelPath error:&tError];
                    if (!tDelOk) {
                        NSLog(@"delete file item error =%@",[tError description]);
                    }
                    if ([tDelPath rangeOfString:tRootVC.m_AudioVC.m_MusicPath].location != NSNotFound) {
                        [tRootVC.m_AudioVC delFileByFTP:tDelPath];
                    }
                }
                [m_EditItems removeAllObjects];
                [self flashItemsToShow];
            }
        }
            break;
        case 24://压缩
        {
            if ([m_EditItems count] > 0) {
                NSString* tName =[m_CurrentPath stringByAppendingPathComponent:[NSString stringWithFormat:@"archive%@.zip",[self nsdateToNSString:[NSDate date]]]];
                [self archiveItems:m_EditItems zipName:tName];
                [m_EditItems removeAllObjects];
                [self checkBottomButton];
                m_OprEditMode = ItemOperNoneMode;
            }
        }
            break;
        case 25://重命名
        {
            if ([m_EditItems count] > 0) {
                m_OprEditMode = ItemOperNoneMode;
                NewNameOrCreaterFloderViewController* tNFLViewController = [[NewNameOrCreaterFloderViewController alloc] init];
                tNFLViewController.m_OperMode = OperMode_ReName;
                NSString* tName = [[m_EditItems lastObject] objectForKey:ItemDic_Name];
                tNFLViewController.m_OperPath = [m_CurrentPath stringByAppendingPathComponent:tName];
                [self.navigationController pushViewController:tNFLViewController animated:YES];
                [tNFLViewController release];
            }
        }
            break;
    }
}

//编辑
-(void) docEditMode:(UIBarButtonItem*)f_sender{
    
    BOOL isEdit = NO;
    if ([f_sender.title isEqualToString:@"编辑"]) {
        f_sender.title =@"完成";
    }else {
        f_sender.title =@"编辑";
        isEdit =YES;
        
    }
    [m_EditItems removeAllObjects];
    for (id tFuckCC in self.navigationController.viewControllers) {
        if ([tFuckCC isKindOfClass:[FileTableViewController class]]) {
            FileTableViewController* tViewCC = (FileTableViewController*)tFuckCC;
            UIToolbar* tInBottomBar = (UIToolbar*)[tViewCC.view viewWithTag:30];
            tViewCC.m_OprEditMode =ItemOperNoneMode;
            [tViewCC checkBottomButton];
            if (isEdit) {
                [tViewCC.m_ListTableView setEditing:NO animated:YES];
                [tInBottomBar setItems:tViewCC.m_SuperEditItems animated:YES];
                tViewCC.m_OprEditMode = ItemOperNoneMode;
            }else {
                [tViewCC.m_ListTableView setEditing:YES animated:YES];
                [tInBottomBar setItems:tViewCC.m_SuperNoEditItems animated:YES];
            }
        }
    }
}

#pragma mark-
#pragma mark UITableViewDelegate

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert; ;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 46.f;
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath 
{
    BOOL tIsEdit = tableView.editing;
    if (tIsEdit) {
        int row  =[indexPath row];
        NSDictionary* tItemDic = [m_CurrentFilesArray objectAtIndex:row];
        [m_EditItems removeObject:tItemDic];
        [self checkBottomButton];
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    int row  =[indexPath row];
    BOOL tIsEdit = tableView.editing;
    NSDictionary* tItemDic = [m_CurrentFilesArray objectAtIndex:row];
    if (tIsEdit) {
        [m_EditItems addObject:tItemDic];
        [self checkBottomButton];
    }else {
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        NSString* tFileType= [tItemDic objectForKey:ItemDic_Type];
        if ([tFileType length] == 0) {
            NSString* tFileName= [tItemDic objectForKey:ItemDic_Name];
            [p_DocSearchBar setText:@""];
            NSString* tNewPath= [m_CurrentPath stringByAppendingPathComponent:tFileName];
            FileTableViewController* tNextVC = [[FileTableViewController alloc] init];
            tNextVC.navigationItem.rightBarButtonItem = self.navigationItem.rightBarButtonItem;
            tNextVC.m_FileImgDic = m_FileImgDic;
            tNextVC.m_CurrentPath = tNewPath;
            tNextVC.m_OprEditMode =m_OprEditMode;
            tNextVC.m_EditItems = m_EditItems;
            [self.navigationController pushViewController:tNextVC animated:YES];
            [tNextVC release];
        }else if([tFileType isEqualToString:@"zip"]){
            UIAlertView* tAlertZip = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您确定需要解压缩当前zip文件吗？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"解压缩", nil];
            [tAlertZip show];
            [tAlertZip release];
            
            if (p_UnZipPath) {
                [p_UnZipPath release];
                p_UnZipPath = nil;
            }
            p_UnZipPath = [tItemDic objectForKey:ItemDic_Path];
            [p_UnZipPath retain];
        }
    }
}
#pragma mark-
#pragma mark UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [m_CurrentFilesArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    int row = [indexPath row];
    static NSString* tNameDeCell = @"doccell";
    UITableViewCell* tCell = nil;
    tCell = [tableView dequeueReusableCellWithIdentifier:tNameDeCell];
    if (tCell == nil) {
        tCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tNameDeCell] autorelease];
        UILabel* tItemName = [[UILabel alloc] initWithFrame:CGRectMake(44, 4, 230, 20)];
        [tItemName setBackgroundColor:[UIColor clearColor]];
        [tItemName setFont:[UIFont systemFontOfSize:15.f]];
        tItemName.tag =CellContent_Name;
        [tCell.contentView addSubview:tItemName];
        [tItemName release];
        
        UILabel* tItemTime = [[UILabel alloc] initWithFrame:CGRectMake(44, 26, 200, 16)];
        [tItemTime setFont:[UIFont systemFontOfSize:11.f]];
        [tItemTime setBackgroundColor:[UIColor clearColor]];
        tItemTime.tag =CellContent_Time;
        [tCell.contentView addSubview:tItemTime];
        [tItemTime release];
        
        UILabel* tSizeOrCount = [[UILabel alloc] initWithFrame:CGRectMake(242, 24, 50, 18)];
        [tSizeOrCount setFont:[UIFont systemFontOfSize:10.f]];
        [tSizeOrCount setBackgroundColor:[UIColor clearColor]];
        [tSizeOrCount setTextAlignment:UITextAlignmentRight];
        tSizeOrCount.tag =CellContent_SizeOrCount;
        [tCell.contentView addSubview:tSizeOrCount];
        [tSizeOrCount release];
        
        UIImageView* tIconView = [[UIImageView alloc] initWithFrame:CGRectMake(4, 4, 34, 34)];
        tIconView.tag =CellContent_Img;
        [tCell.contentView addSubview:tIconView];
        [tIconView release];
    }
    tCell.accessoryType =UITableViewCellAccessoryNone;
    NSDictionary* tFileDic = [m_CurrentFilesArray objectAtIndex:row];
    NSLog(@"%@",[tFileDic description]);
    NSString* tFileName = [tFileDic objectForKey:ItemDic_Name];
    int tSize = [[tFileDic objectForKey:ItemDic_Size] intValue];
    NSString* tCountStr = [NSString stringWithFormat:@"%d",[[tFileDic objectForKey:ItemDic_Count] intValue]];
    NSDate* tTime = [tFileDic objectForKey:ItemDic_ModDate];
    NSString* tFileType= [tFileDic objectForKey:ItemDic_Type];
    NSString* tFilePath = [tFileDic objectForKey:ItemDic_Path];
    
    UILabel* tItemName = (UILabel*)[tCell viewWithTag:CellContent_Name];
    [tItemName setText:tFileName];
    UILabel* tItemTime = (UILabel*)[tCell viewWithTag:CellContent_Time];
    [tItemTime setText:[NSString stringWithFormat:@"修改时间:%@",[self nsdateToNSString:tTime]]];
    UILabel* tItemSCount = (UILabel*)[tCell viewWithTag:CellContent_SizeOrCount];
    
    UIImageView* tIconView = (UIImageView*)[tCell viewWithTag:CellContent_Img];
    NSFileManager* tFileManager = [NSFileManager defaultManager];
    BOOL isDir = NO;
    [tFileManager fileExistsAtPath:tFilePath isDirectory:&isDir];
    if ([tFileType length]== 0 && isDir) {
        tCell.accessoryType =UITableViewCellAccessoryDisclosureIndicator;
        [tIconView setImage:[m_FileImgDic objectForKey:@"doc"]];
        [tItemSCount setText:tCountStr];
    }else if([tFileType isEqualToString:@"zip"]){
        [tIconView setImage:[m_FileImgDic objectForKey:@"zip"]];
        [tItemSCount setText:[self countItemSize:tSize]];
    }else {
        UIImage* tImg =[m_FileImgDic objectForKey:@"fil"];
        [tIconView setImage:tImg];
        [tItemSCount setText:[self countItemSize:tSize]];
    }
    return tCell;
}

#pragma mark-
#pragma mark UIScrollViewDelegate
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    NSLog(@"%f",scrollView.contentOffset.y);
    p_NeedDoAnimate =NO;
    if (scrollView.contentOffset.y < SearchBarShowDis) {
        if (decelerate) {
            p_NeedDoAnimate =YES;
        }
        if (scrollView.contentInset.top == 0) {
            [m_ListTableView  setContentInset:UIEdgeInsetsMake(40, 0, 0, 0)];
            
        }
    }else if( scrollView.contentOffset.y >= SearchBarShowDis){
        if (m_ListTableView.contentInset.top != 0) {
            [m_ListTableView  setContentInset:UIEdgeInsetsMake(0, 0, 0, 0)];
        }
    }
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (p_NeedDoAnimate) {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        [m_ListTableView setContentOffset:CGPointMake(0, -40)];
        [UIView commitAnimations];
    }
}
#pragma mark-
#pragma mark UISearchBarDelegate
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    if ([searchText length] > 0) {
        [m_CurrentFilesArray removeAllObjects];
        NSString* tLowText = [searchText lowercaseString];
        for (NSDictionary* tItemsDic in p_BackUpFilesArray) {
            NSString* tNames = [[tItemsDic objectForKey:ItemDic_Name] lowercaseString];
            if ([tNames rangeOfString:tLowText].location != NSNotFound) {
                [m_CurrentFilesArray addObject:tItemsDic];
            }
        }
        [m_ListTableView reloadData];
    }else {
        [self flashItemsToShow];
    }
}
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    [searchBar setShowsCancelButton:YES animated:YES];
    p_RegistBtt.hidden =NO;
    return YES;
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    
}
- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar
{
    [searchBar setText:@""];
    p_RegistBtt.hidden =YES;
    [searchBar setShowsCancelButton:NO animated:YES];
    [p_DocSearchBar resignFirstResponder];
    [self flashItemsToShow];
}


#pragma mark-
#pragma mark tool
-(void) flashItemsToShow
{
    [self checkBottomButton];
    self.m_CurrentFilesArray= [NSMutableArray arrayWithArray:[p_FItemOper getCurrentFloderList:m_CurrentPath]];
    [m_ListTableView reloadData];
}

-(NSString*) countItemSize:(long long)f_Size
{
    int tKB = f_Size/1024;
    int tMB =tKB/1024;
    NSString* tSizeStr = nil;
    if (tMB > 0) {
        tSizeStr = [NSString stringWithFormat:@"%.2fMB",tKB/1024.f];
    }else {
        tSizeStr = [NSString stringWithFormat:@"%dKB",tKB];
    }
    return tSizeStr;
}

-(NSString*) nsdateToNSString:(NSDate*)f_Date
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    NSTimeZone *timeZone = [NSTimeZone localTimeZone];
    [formatter setTimeZone:timeZone];
    [formatter setDateFormat : @"yyyy年M月d日 H点m分"];
    [formatter autorelease];
    return [formatter stringFromDate:f_Date];
}

-(void) checkBottomButton
{
    UIToolbar* tBottomBar = (UIToolbar*)[self.view viewWithTag:30];
    NSMutableArray* tMItems = [NSMutableArray arrayWithArray:tBottomBar.items];
    if ([tMItems count] != 3) {
        for (UIBarButtonItem* tGrayBar in tMItems) {
            [tGrayBar setTintColor:[UIColor lightGrayColor]];
        }
    }
    if (m_ListTableView.editing) {
        if ([m_EditItems count] > 0) {
            if ([m_EditItems count] == 1) {
                UIBarButtonItem* tReName =[tMItems objectAtIndex:5];
                [tReName setTintColor:[UIColor blackColor]];
            }
            UIBarButtonItem* tItem0 =[tMItems objectAtIndex:0];
            [tItem0 setTintColor:[UIColor blackColor]];
            UIBarButtonItem* tItem2 =[tMItems objectAtIndex:2];
            [tItem2 setTintColor:[UIColor blackColor]];
            UIBarButtonItem* tItem3 =[tMItems objectAtIndex:3];
            [tItem3 setTintColor:[UIColor blackColor]];
            UIBarButtonItem* tItem4 =[tMItems objectAtIndex:4];
            [tItem4 setTintColor:[UIColor blackColor]];
        }
        [tBottomBar setItems:tMItems animated:YES];
    }
}

#pragma mark-
#pragma mark WifiAddFileProtocol
-(void) fileAddByWifiFinish:(NSString*) f_FilePath
{
    AppDelegate* tAppDel = (AppDelegate*)[UIApplication sharedApplication].delegate;
    UINavigationController* tRootNav = (UINavigationController*)tAppDel.window.rootViewController;
    RootViewController* tRootVC = (RootViewController*)[tRootNav.viewControllers objectAtIndex:0];
    if ([f_FilePath rangeOfString:tRootVC.m_AudioVC.m_MusicPath].location == 0) {
        [tRootVC.m_AudioVC performSelector:@selector(fileAddByWifiFinish:)withObject:f_FilePath];
    }
    [self flashItemsToShow];
}
-(void) fileDeleteByWifiFinish:(NSString*) f_FilePath
{
    AppDelegate* tAppDel = (AppDelegate*)[UIApplication sharedApplication].delegate;
    UINavigationController* tRootNav = (UINavigationController*)tAppDel.window.rootViewController;
    RootViewController* tRootVC = (RootViewController*)[tRootNav.viewControllers objectAtIndex:0];
    if ([f_FilePath rangeOfString:tRootVC.m_AudioVC.m_MusicPath].location == 0) {
        [tRootVC.m_AudioVC performSelector:@selector(fileDeleteByWifiFinish:)withObject:f_FilePath];
    }
    [self flashItemsToShow];
}

#pragma mark-
#pragma mark AudiFileChangedByFTP
-(void) addFileByFTP:(NSString*)f_Path
{
    [self flashItemsToShow];
}
-(void) delFileByFTP:(NSString*)f_Path
{

    [self flashItemsToShow];
}
-(void) reNameFileByFTP:(NSString*)f_Path oldName:(NSString*)f_OldName
{
    [self flashItemsToShow];
}

#pragma mark-
#pragma mark UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        //解压缩
        [self unArchiveZipFile:p_UnZipPath];
        [m_EditItems removeAllObjects];
        [self checkBottomButton]; 
    }
}

@end
